new Vue({
    el: '#app',
    data: function () {
        return {
            menuTree: [],
            visible: false,
            isCollapse: true,
            // 选项卡
            editableTabs: [],
            // 选中的选项卡
            editableTabsValue: '',
            homeUrl: '/view/home'
        }
    },
    methods: {
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        // 获取菜单
        treeMenu() {
            treeMenu().then(data => {
                console.log(data)
                this.menuTree = data;
            })
        },
        // 增加选项卡
        addTab(tabName, tabUrl) {
            let isExist = false;
            let tabs = this.editableTabs;
            for (let i = 0; i < tabs.length; i++) {
                if (tabs[i].title === tabName) {
                    isExist = true;
                    break;
                }
            }
            if (isExist) {
                this.editableTabsValue = tabUrl;
                return;
            }
            tabs.push({
                title: tabName,
                name: tabUrl,
                content: tabUrl
            });
            this.editableTabsValue = tabUrl;
        },
        // 关闭选项卡
        removeTab(tabName) {
            let tabs = this.editableTabs;
            let activeName = this.editableTabsValue;
            if (activeName === tabName) {
                tabs.forEach((tab, index) => {
                    if (tab.name === tabName) {
                        let nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.name;
                        }
                    }
                });
            }
            this.editableTabsValue = activeName;
            this.editableTabs = tabs.filter(tab => tab.name !== tabName);
        },
    },
    created() {
        this.treeMenu();
        this.addTab("首页", this.homeUrl)
    }
})